package com.openess.bigsearch.engine.utils;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;

public class HashSet<E> extends AbstractSet<E> implements Set<E> {
	private HashMap<E, Object> map;
	/**
	 * 好处在于，所有使用PRESENT的地方，都使用的是同一个对象
	 */
	private static final Object PRESENT = new Object();

	public HashSet() {
		map = new HashMap<E, Object>();
	}

	public HashSet(int capacity) {
		map = new HashMap<E, Object>(capacity);
	}

	@Override
	public int size() {
		return map.size;
	}

	@Override
	public boolean isEmpty() {
		return map.isEmpty();
	}

	@Override
	public boolean contains(Object o) {
		return map.containsKey(o);
	}

	@Override
	public Iterator<E> iterator() {
		return map.keySet().iterator();
	}

	@Override
	public boolean add(E e) {
		return map.put(e, new Object()) == null;
	}

	@Override
	public boolean remove(Object o) {
		return map.remove(o) == PRESENT;
	}

	@Override
	public void clear() {
		map.clear();
	}
}
